package com.epfresh.supplier.dialog;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;


import com.epfresh.api.global.BaseApplication;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.api.widget.Dialog.BaseDialogView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.MerchantItemAdapter;
import com.epfresh.supplier.entity.MerChantInfo;

import java.util.ArrayList;
import java.util.List;

import multitype.MultiTypeAdapter;
import multitype.OnRecyclerViewItemClickListener;

/**
 * @author wangzhanghuan
 * @version 1.0
 * @date create 17-8-23
 * @Description
 */
public class MerchantListDialog extends BaseDialogView {

    public MerchantListDialog(Context context) {
        super(context);
        setInAnim(true);
        setOutAnim(true);
        inAnim.setDuration(300);
        outAnim.setDuration(300);

    }

   public Animation getInAnimation() {

        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_in);
    }

    public Animation getOutAnimation() {
        return AnimationUtils.loadAnimation(this.context, com.epfresh.api.R.anim.anim_top_push_out);
    }

    @Override
    protected void startInAnimation() {
        if (rv != null) {
            rv.startAnimation(inAnim);
        }
    }

    @Override
    protected void startOutAnimation() {
        if (rv != null) {
            rv.startAnimation(outAnim);
        }
    }

    @Override
    public void initBefore() {
        gravity = Gravity.RIGHT;
    }

   @Override
    protected void initRes() {
        outContainerRes = R.id.f_out_container;
        containerRes = R.id.f_content_container;
        rootRes = R.layout.dialog_content_view;
       rootBgRes = R.id.f_v_content_bg;
    }

   protected ViewGroup getDecorView(Context context) {
        return (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(R.id.ll_root_alter);
    }

    @Override
    protected void createdView() {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View rootView = layoutInflater.inflate(R.layout.dialog_merchant_list, null, false);
        ViewGroup.LayoutParams lp = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, LocalDisplay.dp2px(330));
        rootView.setLayoutParams(lp);
        setContentView(rootView);
//        rootView.setClickable(true);
        setCancelable(true);
        initView();
    }

    RecyclerView rv;
    MultiTypeAdapter multiAdapter;
    MerchantItemAdapter adapter;
    List<MerChantInfo> items = new ArrayList<>();

    private void initView() {

        rv = findView(R.id.rv);
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(layoutManager);
        if (adapter == null)
            adapter = new MerchantItemAdapter();
        if (multiAdapter == null)
            multiAdapter = new MultiTypeAdapter();
        multiAdapter.register(MerChantInfo.class, adapter);
        rv.setAdapter(multiAdapter);
        adapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int i, Object o) {

                dismiss();
                if (onMerchantSwitch != null && o instanceof MerChantInfo){
                    onMerchantSwitch.onMerchant((MerChantInfo) o);
                }
            }
        });


        multiAdapter.setItems(items);
    }

    public void show(List<MerChantInfo> list) {
        items.clear();
        if (list != null && list.size() > 0) {
           String id = BaseApplication.getInstance().getMerchant().getId();
            for (MerChantInfo m : list) {
                m.setSelect((id != null && !"".equals(id) && id.equals(m.getId())));
            }
            items.addAll(list);
            show();
            multiAdapter.notifyDataSetChanged();
        }
    }


    OnMerchantSwitch onMerchantSwitch ;

    public OnMerchantSwitch getOnMerchantSwitch() {
        return onMerchantSwitch;
    }

    public void setOnMerchantSwitch(OnMerchantSwitch onMerchantSwitch) {
        this.onMerchantSwitch = onMerchantSwitch;
    }



    public interface OnMerchantSwitch {
        void onMerchant(MerChantInfo merChantInfo);
    }


}
